package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmLoginDto;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Resource
    private FileStorageService fileStorageService;
    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadImage(MultipartFile multipartFile) {
        //1. 参数校验
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 使用uuid作为文件名
        String filename = UUID.randomUUID().toString().replace("-", "");
        // 获取文件名
        String originalFilename = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String postFix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        //2.将图片上传到minio
        try {
            fileId = fileStorageService.uploadImgFile("", filename + postFix, multipartFile.getInputStream());
            log.info("上传图片到Minio: {}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl--上传文件到Minio失败: {}",fileId);
        }
        //3.保存图片信息到数据库
        WmMaterial material = new WmMaterial();
        material.setUserId(WmThreadLocalUtil.getUser().getId());
        material.setUrl(fileId);
        material.setType((short)0);
        material.setIsCollection((short)0);
        material.setCreatedTime(new Date());
        this.save(material);
        //4.返回图片信息
        return ResponseResult.okResult(material);
    }

    /**
     * 获取图片素材列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        // 1.参数校验
        dto.checkParam();
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();

        if(dto.getIsCollection() != null && dto.getIsCollection() == 1){
            // 查询收藏的图片
            queryWrapper.eq(WmMaterial::getIsCollection,1);
        }
        // 按照用户查询
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        // 按照时间倒序查询
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        // 构造分页参数
        IPage page = new Page<>(dto.getPage(),dto.getSize());
        // 2.分页查询
        page = page(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        // 3.返回结果
        return result;
    }
}
